/**
 * Licensed to the Apache Software Foundation （ASF） under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * （the "License"）； you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 * <p>
 * https://www.q3z3.com
 * QQ : 939313737
 * <p>
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.platform.common.web.intercepors;

import com.platform.common.web.utils.Pager;
import com.platform.common.web.utils.PagerContext;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.ServletRequestUtils;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 分页管理
 */
@Component
public class PagerInterceptor extends HandlerInterceptorAdapter {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {

        Integer page = ServletRequestUtils.getIntParameter(request, "page", 1);
        Integer limit = ServletRequestUtils.getIntParameter(request, "limit", 10);
        // 封装分页信息
        if (page >= 0 && limit >= 0) {
            int offset = (page - 1) * limit;
            PagerContext.setPager(new Pager<>(offset, limit, page));
        }

        return super.preHandle(request, response, handler);
    }
}
